<?php
namespace app\mobile\controller;

use app\mobile\model\Member;
use app\mobile\model\MemberBrokerage;
use think\Controller;
use think\Db;
use think\Exception;
use think\Request;
use think\Session;

/**
 * Class ThirdParty
 * @package app\mobile\controller    第三方登录
 */
class ThirdParty extends Controller
{
    /*
    * 微信登录
    */
    public function wechat_login(Request $request)
    {
        if ($request->isGet()){
            $data = $request->param();
            //换取网页授权access_token
            $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx8b29fa71bc1aa7ea&secret=f264641e7bf08b71eebf42714328fe5b&code=".$data['code']."&grant_type=authorization_code";
            $json = curl_get_contents($url);
            $arr = json_decode($json,true);
            if (array_key_exists('errmsg',$arr)) throw new Exception($arr['errmsg']);
            //获取用户信息
            $url2 = "https://api.weixin.qq.com/sns/userinfo?access_token=".$arr['access_token']."&openid=".$arr['openid']."&lang=zh_CN";
            $info = curl_get_contents($url2);
            $user_arr = json_decode($info,true);
            //去登录
            if (array_key_exists('invit_id',$data)){  //含推荐人的情况
                self::third_login($user_arr,'wechat_id',$data['invit_id']);
            }else{
                self::third_login($user_arr,'wechat_id');
            }
        }
    }
    /*
     * 登录|注册第三方账号
     */
    public function third_login($info,$login_type,$invit_id = 1)
    {
        $model = new Member();
        //  $login_type ['wechat_id','weibo_id','qq_id'];
        $arr = $model->where($login_type,$info['openid'])->find();
        if ($arr){
            $map['login_time'] = time();
            $map['login_ip'] = Request::instance()->ip();
            $map['id'] = $arr['id'];
            $map['password'] = '';
            $result = $model->isUpdate(true)->save($map);
            Session::set('member_id',$arr['id']);
            if (!$result) throw new Exception('登录异常,请重试');
            $this->redirect('UserInfo/my_shop_index');  //跳转到我的
        }else{   //去注册
            $has = $model->where($login_type,$info['openid'])->find();
            if ($has){
                //更改unionid
                $model->where($login_type,$info['openid'])->update(['wechat_id' => $info['openid']]);
            }else{
                $ini['nickname'] = removeEmoji($info['nickname']);
                $ini['password'] = '';
                $ini['wechat_id'] = $info['openid'];
                $ini['avatar'] = $info['headimgurl'];
                $ini['create_time'] = time();
                $ini['create_ip'] = Request::instance()->ip();
                $ini['suffer'] = 1;
                $model->allowField(true)->save($ini);
                //不含推荐人 默认是老板id 1
                $model->distribution($invit_id,$model->id);
                Session::set('member_id',$model->id);
                //附加会员佣金
                self::add_brokerage($model->id);
            }
            $this->redirect('UserInfo/my_shop_index');  //跳转到我的
        }
    }
    /*
     * 附加会员佣金表
     */
    public function add_brokerage($member_id)
    {
        $model = new MemberBrokerage();
        $map['member_id'] = $member_id;
        $map['general_income'] = 0 ;
        $map['to_yield'] = 0 ;
        $map['to_audit'] = 0 ;
        $map['actual_amount'] = 0 ;
        $model->save($map);
    }
}